<!--新增系统用户-->
<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <el-form ref="form" :model="detail" label-width="80px" class="form">
        <el-form-item label="名称">
          <el-input v-model="detail.title" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input
            v-model="detail.sort"
            @input="detail.sort = detail.sort.replace(/[^\.\d]/g,'').replace('.','')"
          />
        </el-form-item>
        <el-form-item label="序列号">
          <el-input v-model="detail.simplify" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="detail.remark" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { areaAdd, areaUpdate } from '@/api/api'
export default {
  data() {
    return {
      detail: {
        'remark': '',
        'simplify': '',
        'sort': 0,
        'title': ''
      }
    }
  },
  created() {
    if (this.$route.query.add) {
      const item = JSON.parse(this.$route.query.add)
      this.detail.parentId = item.id
      this.detail.level = item.level + 1
    }
    if (this.$route.query.edit) {
      const item = JSON.parse(this.$route.query.edit)
      this.detail.id = item.id
      this.detail.parentId = item.parentId
      this.detail.level = item.level
      this.detail.remark = item.remark
      this.detail.simplify = item.simplify
      this.detail.sort = item.sort
      this.detail.title = item.title
    }
  },
  methods: {
    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      if (!this.vertify()) {
        return false
      }
      if (this.$route.query.edit) {
        // 编辑
        areaUpdate(this.detail).then(data => {
          Is.tip('编辑成功', 2000, 'success')
          // this.$router.back()
		  
        })
      } else if (this.$route.query.add) {
        // 新增
        areaAdd(this.detail).then(data => {
          Is.tip('添加成功', 2000, 'success')
          // this.$router.back()
		  this.detail={
		    'remark': '',
		    'simplify': '',
		    'sort': 0,
		    'title': ''
		  }
        })
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      // if (Is.empty(this.detail.address)) {
      //   Is.tip('请输入地址')
      //   return false
      // }
      return true
    }
  }}
</script>
